<template>
  <div>


    <div class="pd-10">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="username"></el-input>
      <el-input style="width: 200px" placeholder="请输入邮箱" suffix-icon="el-icon-message" v-model="email" class="ml-5"></el-input>
      <el-input style="width: 200px" placeholder="请输入地址" suffix-icon="el-icon-s-promotion" v-model="address" class="ml-5"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
    </div>

    <div class="mg-10">
      <el-button type="primary" @click="handleAdd">新增<i class="el-icon-circle-plus-outline ml-5"></i></el-button>

      <el-popconfirm
          confirm-button-text='是的我确定'
          cancel-button-text='算了在想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定删除吗？"
          @confirm="delBatch"
          class="ml-5"
      >
        <el-button type="danger" slot="reference">批量删除<i class="el-icon-remove-outline ml-5"></i></el-button>
      </el-popconfirm>
      <el-upload :action="serverIp+'/user/import'"
                 style="display: inline-block"
                 accept="xlsx"
                 :show-file-list="false"
                 :on-success="handleExcelImportSuccess"
      >
        <el-button type="primary" class="ml-5">导入<i class="el-icon-bottom ml-5"></i></el-button>
      </el-upload>

      <el-button type="primary" @click="exp" class="ml-5">导出<i class="el-icon-top ml-5"></i></el-button>
    </div>

    <el-table
        :data="tableData" border stripe
        :header-cell-style="{ background:'#eee',border: '1px solid #ccc' ,color: '#000'}"
        @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="140">
      </el-table-column>
      <el-table-column prop="role" label="角色">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.role === 'ROLE_ADMIN'">管理员</el-tag>
          <el-tag type="warning" v-if="scope.row.role === 'ROLE_STUDENT'">学生</el-tag>
          <el-tag type="success" v-if="scope.row.role === 'ROLE_TEACHER'">老师</el-tag>
          <el-tag type="info" v-if="scope.row.role === 'ROLE_USER'">普通用户</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" width="120">
      </el-table-column>
      <el-table-column prop="phone" label="手机号">
      </el-table-column>
      <el-table-column prop="email" label="电子邮箱">
      </el-table-column>
      <el-table-column prop="address" label="地址">
      </el-table-column>
      <el-table-column label="操作" width="350">
        <template slot-scope="scope" >
          <el-button type="info" @click="lookCourse(scope.row.courses)" v-if="scope.row.role === 'ROLE_TEACHER'">查看课程<i class="el-icon-edit ml-5"></i></el-button>
          <el-button type="warning" @click="lookStudentCourse(scope.row.studentCourses)" v-if="scope.row.role === 'ROLE_STUDENT'">查看已选修课程<i class="el-icon-edit ml-5"></i></el-button>
          <el-button type="success" @click="handleEdit(scope.row)" >编辑<i class="el-icon-edit ml-5"></i></el-button>
          <el-popconfirm
              confirm-button-text='是的我确定'
              cancel-button-text='算了在想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="handledelete(scope.row.id)"
              class="ml-5"
          >
            <el-button type="danger" slot="reference">删除<i class="el-icon-delete ml-5"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0;">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="新增用户" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form"  label-width="80px" size="small">
        <el-form-item label="用户名" >
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户名" >
          <el-select v-model="form.role"  clearable placeholder="请选择" style="width: 100%">
            <el-option v-for="item in roles" :key="item.name" :label="item.name" :value="item.flag">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="密码" >
          <el-input v-model="form.password" autocomplete="off" show-password></el-input>
        </el-form-item>

        <el-form-item label="昵称" >
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" >
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" >
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" >
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>


    <el-dialog title="已教课程" :visible.sync="dialogSourseVisible" width="30%">
        <el-table :data="courses" border stripe>
          <el-table-column prop="name" label="课程名称"></el-table-column>
          <el-table-column prop="score" label="学分"></el-table-column>
        </el-table>
    </el-dialog>

    <el-dialog title="已选修课程" :visible.sync="dialogStudentVisible" width="30%">
      <el-table :data="studentCourses" border stripe>
        <el-table-column prop="name" label="课程名称"></el-table-column>
        <el-table-column prop="score" label="学分"></el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import {serverIp} from "../../../public/config";

export default {
  name: "User",
  data(){
    return{
      serverIp: serverIp,
      tableData: [], //分页数据
      total: 0, //总条数
      pageNum: 0,
      pageSize: 5,
      username: "", //搜索框用户名
      email:"", //搜索框电子邮箱
      address:"", //搜索框地址查询
      form: {}, //表格数据
      multipleSelection: [], //多选删除
      dialogFormVisible: false, //新增按钮
      dialogSourseVisible: false,
      dialogStudentVisible: false, //学生查看选修课
      roles:[],
      courses:[], //老师的课程信息
      studentCourses:[], //学生的选课
    }
  },
  created() {
    this.load()
  },
  methods:{
    //老师查看课程
    lookCourse(courses){
      this.courses = courses;
      this.dialogSourseVisible = true;
    },
    //学生查看选修课
    lookStudentCourse(studentCourses){
      this.studentCourses = studentCourses;
      this.dialogStudentVisible = true;
    },
    //分页请求
    load(){
      this.request.get("/user/page",{
        params:{
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          username: this.username,
          email: this.email,
          address: this.address
        }
      }).then(res =>{
        if(res.code === "200"){
          this.tableData = res.data.data
          this.total = res.data.total
        }else {
          this.$message.error(res.msg)
        }
      })

      this.request.get("/role").then(res=>{
        this.roles=res.data;
      })

    },
    //分页方法
    handleSizeChange(pageSize) {
      this.pageSize=pageSize;
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load()
    },
    //重置搜索框
    reset(){
      this.username = "";
      this.address = "";
      this.email = "";
      this.load()
    },
    //新增按钮
    handleAdd(){
      this.dialogFormVisible = true;
      this.form = {}
    },
    //确定新增
    save(){
      this.request.post("/user",
          this.form).then(res=>{
        if(res.code ==="200"){
          this.$message.success(res.data)
          this.form ={}
          this.dialogFormVisible = false;
          this.load()
        }else {
          this.$message.error(res.msg)
        }
      })
    },
    //编辑按钮
    handleEdit(row){
      this.form = row
      this.dialogFormVisible = true
    },
    //取消按钮
    cancel() {
      this.dialogFormVisible=false
      this.load()
    },
    //删除按钮
    handledelete(id){
      this.request.delete("/user/"+id).then(res=>{
        if(res.code === "200"){
          this.$message.success(res.data)
          this.load()
        }else {
          this.$message.error(res.msg)
        }
      })
    },
    //批量删除多选
    handleSelectionChange(val){
      this.multipleSelection  = val
    },
    //点击批量删除
    delBatch(){
      let ids= this.multipleSelection.map(v => v.id)  //将数组内的id提取出来
      this.request.delete("/user/del/batch",{
        data: ids
      }).then(res=>{
        if(res.code === "200"){
          this.$message.success(res.data)
          this.load()
        }else {
          this.$message.error(res.msg)
        }
      })
    },
    //导出excel
    exp(){
      window.open("http://"+this.serverIp+":8089/user/export")
    },
    //导入成功后
    handleExcelImportSuccess(){
      this.$message.success("文件导入成功")
      this.load()
    }
  }
}
</script>

<style scoped>

</style>